package com.qf.blog.service.impl;

import com.qf.blog.auth.LoginThreadLocal;
import com.qf.blog.entity.User;
import com.qf.blog.vo.StarsVo;
import io.netty.util.internal.ObjectUtil;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class FollowerService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String  followerKey = "blog:follower:%s";

    private String  fansKey = "blog:fans:%s";

    private String startKey = "blog:stars:%s:%s";

    private String userKey = "blog:user:%s";

    public void follower(Integer loginUserId, String userId) {
        String fowKey = String.format(followerKey, loginUserId);
        String fanKey = String.format(fansKey, userId);
        Boolean IsFollower = IsFollower(loginUserId, userId);
        if(IsFollower){
            stringRedisTemplate.opsForZSet().remove(fowKey,userId);
            stringRedisTemplate.opsForZSet().remove(fanKey,loginUserId);
        }else {
        stringRedisTemplate.opsForZSet().add(fowKey,userId,System.currentTimeMillis());
        stringRedisTemplate.opsForZSet().add(fanKey,loginUserId.toString(),System.currentTimeMillis());
        }
    }

    public Boolean IsFollower(Integer loginUserId, String userId) {
        String rFowKey = String.format(followerKey, loginUserId);
        Double score = stringRedisTemplate.opsForZSet().score(rFowKey,userId);
        return score != null;
    }

    public Long getFollowerCount(Integer userId) {
        String fowKey = String.format(followerKey, userId);
        return stringRedisTemplate.opsForZSet().size(fowKey);
    }

    public Long getFansCount(Integer userId) {
        String fanKey = String.format(fansKey, userId);
        return stringRedisTemplate.opsForZSet().size(fanKey);
    }

    public Map<String,Object> starts(StarsVo starsVo) {
        Map<String,Object> map = new HashMap<>();

        String starKey = String.format(startKey,starsVo.getEntityType(),starsVo.getEntityId());
        String uKey = String.format(userKey, starsVo.getEntityUserId());

        Double score = stringRedisTemplate.opsForZSet().score(starKey, starsVo.getUserId().toString());
        if (score != null) {
            //已经点赞
            stringRedisTemplate.opsForZSet().remove(starKey, starsVo.getUserId().toString());
            map.put("likeStatus",0);
            stringRedisTemplate.opsForValue().decrement(uKey);
        }else {
            //还没有点赞
            stringRedisTemplate.opsForZSet().add(starKey, starsVo.getUserId().toString(), System.currentTimeMillis());
            map.put("likeStatus",1);
            stringRedisTemplate.opsForValue().increment(uKey);
        }
        Long size = getStartCount(starsVo.getEntityType(), starsVo.getEntityId());
        map.put("likeCount", size);
        return map;
    }

    public Long getStartCount(Integer entityType,Integer entityId) {
        String starKey = String.format(startKey,entityType,entityId);
        return stringRedisTemplate.opsForZSet().size(starKey);
    }

    public Integer IsStars(Integer entityType,Integer entityId) {
        User user = LoginThreadLocal.get();
        if (user != null) {
            Integer loginId =user.getId();
            String starKey = String.format(startKey,entityType,entityId);
            Double score = stringRedisTemplate.opsForZSet().score(starKey, loginId.toString());
            return score !=null ? 1 : 0;
        }
        return 0;
    }

    public Long getStarsCount(Integer userId) {
        String uKey = String.format(userKey, userId);
        String size = stringRedisTemplate.opsForValue().get(uKey);
        if (!ObjectUtils.isEmpty(size)){
            return Long.parseLong(size);
        }
        return 0L;
    }
}
